package com.miaosha.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.miaosha.dataobject.ProductDO;
import com.miaosha.error.EnumError;
import com.miaosha.error.ErrorException;
import com.miaosha.model.ProductModel;
import com.miaosha.model.PromoModel;
import com.miaosha.model.UserInformationModel;
import com.miaosha.model.viewobject.ProductVO;
import com.miaosha.response.CommonResponse;
import com.miaosha.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.stream.Collectors;

@Controller()
@RequestMapping("/product")
public class ProductController extends BaseController{

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/createProduct", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORM})
    @ResponseBody
    public CommonResponse createProduct(@RequestParam(name = "title")String title,
                                   @RequestParam(name = "description")String description,
                                   @RequestParam(name = "price")BigDecimal price,
                                   @RequestParam(name = "stock")Integer stock,
                                   @RequestParam(name = "imageUrl")String imageUrl) throws ErrorException {
        // 用户注册流程
        ProductModel productModel = new ProductModel();
        productModel.setTitle(title);
        productModel.setDescription(description);
        productModel.setPrice(price);
        productModel.setStock(stock);
        productModel.setImageUrl(imageUrl);

        ProductModel productFromCreate = productService.createProduct(productModel);
        ProductVO productVO = convertProductVOFromProductModel(productFromCreate);
        return CommonResponse.create(productVO);
    }

    @RequestMapping(value = "/getProductDetail", method = {RequestMethod.GET})
    @ResponseBody
    public CommonResponse getProductDetail(@RequestParam(name = "productId")Integer productId) throws ErrorException {
        if (productId == null) {
            throw new ErrorException(EnumError.PARAMETER_VALIDATION_ERROR);
        }
        ProductModel productModel = productService.getProductDetailById(productId);
        if (productModel == null){
            throw new ErrorException(EnumError.PRODUCT_NOT_EXIST);
        }
        ProductVO productVO = convertProductVOFromProductModel(productModel);
        return CommonResponse.create(productVO);
    }

    @RequestMapping(value = "/getProductList", method = {RequestMethod.GET})
    @ResponseBody
    public CommonResponse getProductList() throws ErrorException {
        List<ProductModel> productModelList = productService.getProductList();
        List<ProductVO> productVOList = productModelList.stream().map(productModel -> {
            ProductVO productVO = convertProductVOFromProductModel(productModel);
            return productVO;
        }).collect(Collectors.toList());
        return CommonResponse.create(productVOList);
    }

    private ProductVO convertProductVOFromProductModel(ProductModel productModel){
        if (productModel == null) return null;
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(productModel, productVO);
        if (productModel.getPromoModel() != null){
            PromoModel promoModel = productModel.getPromoModel();
            productVO.setPromoId(promoModel.getId());
            productVO.setPromoStatus(promoModel.getStatus());
            productVO.setPromoPrice(promoModel.getPromoProductPrice());
            productVO.setStartDate(promoModel.getStartDate().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
        } else {
            productVO.setPromoStatus(0);
        }
        return productVO;
    }
}
